<template>
  <vxe-modal
    :title="$t('common.button.setting', '设置')"
    v-model="show"
    :loading="loading"
    :mask="false"
    :lock-view="false"
    width="800px"
    height="300px"
    destroy-on-close
    show-zoom
    remember
    resize
    show-footer
  >
    <!-- 主体部分： form 表单 -->
    <el-form
      ref="refForm"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      label-position="top"
    >
      <!-- remark 设置说明 -->
      <el-form-item>
        <span class="text-primary">{{ formData.name }}</span>
        <span class="text-secondary"> （{{ formData.remark }}） </span>
      </el-form-item>

      <!-- value 设置值 -->
      <el-form-item prop="value">
        <!-- input -->
        <template v-if="formData.type === 'input'">
          <el-input v-model="formData.value" />
        </template>

        <!-- textarea -->
        <template v-if="formData.type === 'textarea'">
          <el-input type="textarea" v-model="formData.value" />
        </template>

        <!-- radio -->
        <template v-if="formData.type === 'radio'">
          <ra-dict-select
            v-model="formData.value"
            type="radio"
            :code="formData.config"
          />
        </template>

        <!-- checkbox -->
        <template v-if="formData.type === 'checkbox'">
          <ra-dict-select
            v-model="formData.value"
            type="checkbox"
            :code="formData.config"
          />
        </template>

        <!-- select-single -->
        <template v-if="formData.type === 'select_single'">
          <ra-dict-select
            v-model="formData.value"
            type="select"
            :code="formData.config"
            :multiple="false"
          />
        </template>

        <!-- select-multiple -->
        <template v-if="formData.type === 'select_multiple'">
          <ra-dict-select
            v-model="formData.value"
            type="select"
            :code="formData.config"
            :multiple="true"
          />
        </template>

        <!-- image_upload_single -->
        <template v-if="formData.type === 'image_upload_single'">
          <ra-upload-card
            v-model="formData.value"
            :multiple="false"
            :limit="1"
          />
        </template>

        <!-- image_upload_multiple -->
        <template v-if="formData.type === 'image_upload_multiple'">
          <ra-upload-card
            v-model="formData.value"
            :multiple="true"
            :limit="6"
          />
        </template>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button type="warning" @click="cancelHandler">
        {{ $t('common.action.cancel', '取消') }}
      </el-button>
      <el-button type="info" @click="resetHandler">
        {{ $t('common.action.reset', '重置') }}
      </el-button>
      <el-button type="primary" @click="handleSubmit" :disabled="loading">
        {{ $t('common.action.confirm', '确定') }}
      </el-button>
    </template>
  </vxe-modal>
</template>
<script setup>
import { smartTranslate as $t, transPlaceholder as $p } from '@/lang';

defineOptions({
  name: 'ConfigSetting',
  inheritAttrs: false,
});

const props = defineProps({
  api: {
    type: Object,
    required: true,
    validator: (value) => {
      return typeof value.update === 'function';
    },
  },
  options: {
    type: Object,
    default: () => ({}),
  },
});

// 定义可触发事件 update 此事件触发后，会执行绑定到此事件上的父组件函数
const emit = defineEmits(['reload']);
// 是否显示载入效果
const loading = ref(false);
// 是否显示
const show = ref(false);

/**
 * 表单相关
 */
// 表单对象
const refForm = ref(null);
// 表单数据
const formData = reactive({
  id: null,
  name: null,
  key: null,
  value: null,
  type: null,
  config: null,
  remark: null,
});
// 表单验证规则
const formRules = reactive({});

/**
 * 方法
 */
// 重置表单
const resetForm = () => {
  if (refForm.value) {
    refForm.value.resetFields();
  }
};

/**
 * 操作句柄
 */
// 更新操作
function handleSubmit() {
  refForm.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      props.api
        .update(formData)
        .then(() => {
          ElMessage.success(
            $t('common.message.info.operate_success', '操作成功'),
          );
          resetForm();
          show.value = false;
          emi$t('reload');
        })
        .finally(() => (loading.value = false));
    }
  });
}

// 取消操作
const cancelHandler = () => {
  resetForm();
  show.value = false;
};

// 重置表单操作
const resetHandler = () => {
  resetForm();
};

/**
 * 暴露方法
 */
defineExpose({
  show(id) {
    const params = { id: id };
    props.api.getFormData(params).then(({ data }) => {
      Object.assign(formData, data);
      nextTick(() => {
        show.value = true;
        // 重置表单
        resetForm();
      });
    });
  },
});
</script>
<style lang="scss" scoped>
:deep(.el-tabs__header) {
  display: none;
}
:deep(.el-tabs__content) {
  padding: 0 !important;
}
</style>
